<template>
	<view class="container">
		<view class="custom-navbar" :style="{ height: navHeight + 'px', paddingTop: statusBarHeight + 'px' }">
			<view class="nav-left" @click="goBack">
				<image class="back-icon" src="/static/img/arrow-right.png"></image>
			</view>
		</view>


		<view class="coupon-index" :style="{ backgroundImage: 'url(' + bg_pic + ')' }">
			<block v-if="couponlist&& couponlist.length>0">

				<scroll-view class="coupon-box" scroll-y>
					<view class="card" v-for="(item, index) in couponlist" :key="index" @tap.stop="goto">
						<view class="card-left">
							<view class="pt_left">
								<view class="pt_left-content">
									<view class="f1" v-if="item.type == 1"><text class="t0">￥</text><text class="t1">{{
								item.money }}</text></view>
									<view class="f1" v-if="item.type == 41"><text class="t0">￥</text><text class="t1">{{
								item.money }}</text></view>
									<view class="f1" v-if="item.type == 30"><text class="t0">￥</text><text class="t1">{{
								item.money }}</text></view>
									<view class="f1" v-if="item.type == 10"><text class="t1">{{ item.discount / 10
							}}</text><text class="t0">折</text></view>
									<view class="f1" v-if="item.type == 2">礼品券</view>
									<view class="f1" v-if="item.type == 3"><text class="t1">{{ item.limit_count
							}}</text><text class="t2">次</text></view>
									<view class="f1" v-if="item.type == 4">抵运费</view>
									<view class="f1" v-if="item.type == 5"><text class="t0">￥</text><text class="t1">{{
								item.money }}</text></view>
									<view class="f1" v-if="item.type == 40">电影通用兑换券</view>
									<view class="f1" v-if="item.type == 20">券包</view>
								</view>
							</view>
						</view>
						<view class="card-item">
							<view class="card-right">
								<view class="card-time fw-700">
									{{ item.name }}
								</view>
								<view class="card-time">
									<text class="t2" v-if="item.type == 1">商品券</text>
									<text class="t2" v-if="item.type == 41">新人券</text>
									<text class="t2" v-if="item.type == 30">当面付抵扣券</text>
									<text class="t2" v-if="item.type == 2">礼品券</text>
									<text class="t2" v-if="item.type == 3">计次券</text>
									<text class="t2" v-if="item.type == 4">运费抵扣券</text>
									<text class="t2" v-if="item.type == 20">券包</text>
									<text class="t2" v-if="item.type == 40">电影通用兑换券</text>
									<view class="">
										<text class="t2"
											v-if="!item.from_mid && (item.isgive == 1 || item.isgive == 2)">可赠送</text>
									</view>

									<view class="t4" v-if="item.bid > 0">适用商家：{{ item.bname }}</view>
								</view>
								<view class="card-time">有效期至 {{ item.endtime }}</view>
							</view>
						</view>
					</view>
					<view class="fixed-bottom">
						<image class="get-btn" @click="handleGetCoupon" v-if="data_item.haveget==0"
							src="https://hedashepin.oss-cn-guangzhou.aliyuncs.com/upload/3/20250510/f7c22e48b755aada712ba4a5a4ee498d.png"
							mode=""></image>
						<image class="get-btn" v-if="data_item.haveget==1"
							src="https://hedashepin.oss-cn-guangzhou.aliyuncs.com/upload/3/20250510/4ba37f4db2906592157ece3ad65ff769.png"
							mode=""></image>
						<!-- <button class="get-btn" @click="handleGetCoupon" v-if="data_item.haveget==0"
						:style="{ background: t('color1') }">点击立即领取</button>
					<button class="get-btn get-btn-disabled" v-if="data_item.haveget==1" disabled
						:style="{ background: t('color2') }">
						已领取
					</button> -->
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	var app = getApp()
	export default {
		data() {
			return {
				data_item: {},
				shareData: {},
				couponlist: [],
				opt: {},
				needRefresh: false,
				bg_pic: '',
				navHeight: 44, // 导航栏内容高度（不包括状态栏）
				statusBarHeight: 20, // 状态栏高度
				menuButtonInfo: {}, // 胶囊按钮信息

			}
		},
		onLoad(opt) {
			this.opt = app.getopts(opt);
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取系统信息
			const systemInfo = uni.getSystemInfoSync()
			this.statusBarHeight = systemInfo.statusBarHeight || 20

			// 获取胶囊按钮信息（仅微信小程序）
			if (uni.getMenuButtonBoundingClientRect) {
				const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
				this.menuButtonInfo = menuButtonInfo
				// 计算导航栏高度 = 状态栏高度 + 胶囊按钮高度 + (胶囊按钮上边距 - 状态栏高度) * 2
				this.navHeight = menuButtonInfo.bottom + (menuButtonInfo.top - this.statusBarHeight)
			} else {
				// 非微信小程序，使用默认高度
				this.navHeight = this.statusBarHeight + 44
			}
			// #endif
			this.getlist();
		},

		onShow() {

			if (this.needRefresh) {
				this.getlist();
				this.needRefresh = false;
			}
		},
		methods: {
			getlist() {
				if (this.loading) return

				this.loading = true
				let that = this

				uni.showLoading({
					title: '加载中'
				})

				app.get('Coupon/getCouponActiveDetial', {
					coupon_active_id: that.opt.coupon_active_id
				}, function(res) {
					uni.hideLoading()
					that.loading = false

					if (res.code == 200) {
						that.data_item = res.data;
						that.couponlist = res.data.couponlist;
						that.bg_pic = res.data.pic;
					} else {
						uni.showToast({
							title: res.msg || '加载失败',
							icon: 'none'
						})
					}
				}, function(err) {
					uni.hideLoading()
					that.loading = false
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					})
				})
			},

			handleGetCoupon() {
				let that = this;
				uni.showLoading({
					title: '领取中...'
				});
				app.post('Coupon/putCouponActive', {
					coupon_active_id: that.opt.coupon_active_id
				}, function(res) {
					if (res.code == 200) {
						app.success(res.msg);
						that.needRefresh = true;
						setTimeout(() => {
							uni.navigateTo({
								url: '/pagesExt/coupon/mycoupon',
								success: () => {

								},
								fail: (err) => {

									uni.navigateTo({
										url: '/pagesExt/coupon/mycoupon'
									});
								}
							});
						}, 2000);
					} else {
						uni.showToast({
							title: res.msg || '加载失败',
							icon: 'none'
						})
					}
				}, function(err) {
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					})
				})
			},
			// 下拉刷新
			onPullDownRefresh() {
				this.page = 1
				this.noMore = false
				this.getlist()
				setTimeout(() => {
					uni.stopPullDownRefresh()
				}, 1000)
			},
			goBack() {
				uni.navigateBack({
					delta: 1,
					fail: (err) => {
						// 如果返回失败(没有上一页)，则跳转到首页
						if (err.errMsg.includes("fail")) {
							uni.navigateTo({
								url: '/pages/my/usercenter'
							});
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.custom-navbar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;

		display: flex;
		align-items: center;
		padding-left: 30rpx;
		padding-right: 30rpx;
		z-index: 999;

	}

	.nav-left {
		width: 60rpx;

	}

	.back-icon {
		width: 40rpx;
		height: 40rpx;
	}

	.coupon-index {

		background-color: #fff;
		min-height: 100vh;
		background-size: cover;
		background-position: center;
		background-repeat: no-repeat;
		position: relative;
	}

	.coupon-title {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.active-img {
		width: 216rpx;
		height: 246rpx;
	}

	.active-text {
		font-size: 40rpx;
		font-weight: bold;
		margin-left: 20rpx;
	}

	.coupon-box {
		position: absolute;
		top: 30%;
		left: 0;
		right: 0;
		bottom: 0;
		/* 给底部按钮留空间 */
		background-color: #fff;
		padding: 20rpx;
		border-radius: 20rpx 20rpx 0 0;
		overflow: hidden;
		/* 重要：确保圆角效果 */
	}

	.coupon-list {
		padding: 20rpx;

		background-color: #AD62D7;
		box-shadow: 0px 4rpx 12rpx 0px rgba(0, 0, 0, 0.4);

		margin-bottom: 40rpx;
		display: flex;

		border-radius: 8px;
		-webkit-mask-image: radial-gradient(circle at 88px 4px, transparent 4px, red 4.5px), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);
		/* -webkit-mask-size: 100%, 2px 4px; */
		-webkit-mask-repeat: repeat, repeat-y;
		-webkit-mask-position: 0 -8rpx, 168rpx;
		color: #fff;
	}

	.coupon-list-left {
		margin-right: 50rpx;
		position: relative;

		display: flex;
		align-items: center;
		justify-content: center;
	}

	.coupon-list-left-img {
		width: 100%;
		height: 100%;
	}

	.money-overlay {
		position: absolute;
		bottom: 20rpx;
		left: 0;
		right: 0;
		text-align: center;
		color: rgba(16, 16, 16, 1);
		font-size: 36rpx;
		font-weight: bold;

	}

	.coupon-list-right {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
	}

	.coupon-list-right-title {

		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.coupon-list-right-time {

		font-size: 26rpx;
	}

	.coupon-list-right-btn {
		display: flex;
		justify-content: flex-end;
		gap: 20rpx;
	}

	.right-btn {
		border: 1px solid rgba(187, 187, 187, 1);
		color: rgba(16, 16, 16, 1);
		font-size: 28rpx;
		width: 160rpx;
		height: 54rpx;
		text-align: center;
		line-height: 54rpx;
		border-radius: 56rpx;
		margin: unset;
	}

	.load-more {
		text-align: center;
		padding: 20rpx;
		font-size: 24rpx;
		color: #999;
	}

	.fixed-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
		/* iOS 11.0 */
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		/* iOS 11.2+ */
		z-index: 999;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.get-btn {
		width: 70%;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 44rpx;
		border-radius: 60rpx;
		border: none;
		background-color: #D01BFF;
		color: #fff;
		font-weight: bold;
	}

	.get-btn::after {
		border: none;
	}

	/* 防止内容被底部按钮遮挡 */
	.coupon-index {
		padding-bottom: 120rpx;
		/* 按钮高度+padding */
	}

	.get-btn-disabled {
		background: #CDABD8 !important;

		color: #fff !important;

		cursor: not-allowed;
		border: 4rpx solid #F4C3F5;

	}

	.card {
		display: flex;
		align-items: center;
		width: 100%;
		background-color: #AD62D7;

		border-radius: 8px;
		-webkit-mask-image: radial-gradient(circle at 88px 4px, transparent 4px, red 4.5px), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);
		/* -webkit-mask-size: 100%, 2px 4px; */
		-webkit-mask-repeat: repeat, repeat-y;
		-webkit-mask-position: 0 -8rpx, 168rpx;

		position: relative;
		margin-bottom: 20rpx;
	}

	.card-left {
		width: 88px;
		text-align: center;
		font-size: 18px;
		color: #fff;

	}

	.card-left-top {
		position: absolute;
		top: 0;
		left: 0;
		font-size: 24rpx;
		padding: 4rpx 12rpx;
		border-bottom-right-radius: 30rpx;
	}

	.card-right {
		padding: 16px 12px;
		display: flex;

		flex-direction: column;
	}

	.card-info {
		margin: 0;
		font-size: 14px;
		line-height: 20px;
		color: #fff;
	}

	.card-time {
		font-size: 12px;
		line-height: 16px;
		font-weight: normal;
		color: #fff;
		margin-top: 4px;
	}

	.card-right-text {
		display: flex;
		align-items: center;
		color: #fff;
	}

	.card-right-img {
		width: 30rpx;
		height: 30rpx;
	}

	.card-left-money {
		font-size: 24rpx
	}

	.card-btn-box {}

	.card-btn {
		color: #fff;
		border-radius: 30rpx;
		padding: 10rpx 30rpx;
		font-size: 24rpx;
		text-align: center;
	}

	.card-btn-text {
		font-size: 24rpx;
		color: #fff;
	}

	.card-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex: 1;
		padding: 0 20rpx 0 0;
	}

	.card-item::before {
		content: '';
		position: absolute;

		top: 0;
		bottom: 0;
		width: 2px;
		border-left: 1px dashed #fff;

	}
</style>